﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApplicationGDI3._1
{
    public class AnimatedSprite
    {
        Bitmap sprite;
        int spriteWidth;
        int spriteHeight;
        int frames, lines;
        int frame = 0, line = 0;
        bool switchLines;

        public int ImageWidth()
        {
            return spriteWidth;
        }

        public int ImageHeight()
        {
            return spriteHeight;
        }

        public Image Image
        {
            get
            {
                return sprite;
            }
        }

        public AnimatedSprite(string filename, int framesX, int framesY, bool switchLines)
        {
            sprite = Bitmap.FromFile(filename) as Bitmap;
            spriteWidth = sprite.Width / framesX;
            spriteHeight = sprite.Height / framesY;
            frames = framesX;
            lines = framesY;
            this.switchLines = switchLines;
        }

        public void SetLine(int line)
        {
            if (line < 0 || line >= lines)
            {
                throw new ArgumentException("Неверное значение линии!");
            }
            this.line = line;
        }

        public Rectangle GetAnimationFrame()
        {
            if (switchLines && line == lines)
            {
                line = 0;
            }
            if (frame == frames)
            {
                frame = 0;
                if (switchLines)
                {
                    line++;
                }
            }

            Rectangle aFrame = new Rectangle(spriteWidth * frame, spriteHeight * line, spriteWidth, spriteHeight);  // класс прямоугольника который мы создаем

            frame++;
            return aFrame;
        }
    }
}
